package com.dindang.propertyfee.mvp.presenter;

import android.app.Application;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.anlia.library.group.GroupItem;
import com.anlia.library.group.GroupItemDecoration;
import com.dindang.propertyfee.R;
import com.dindang.propertyfee.app.AppConst;
import com.dindang.propertyfee.mvp.model.entity.PropertyCharge;
import com.dindang.propertyfee.mvp.ui.adapter.custom.FeeRecordViewAdapter;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import com.dindang.propertyfee.mvp.contract.FeeReceivedContract;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@ActivityScope
public class FeeReceivedPresenter extends BasePresenter<FeeReceivedContract.Model, FeeReceivedContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    private List<PropertyCharge.Record4Client.FeeList> mList = new ArrayList<>();
    Map<String, PropertyCharge.TotalData> mTotalDataMaps = new LinkedHashMap<>();
    private FeeRecordViewAdapter mAdapter;
    private boolean showLabel = true;

    @Inject
    public FeeReceivedPresenter(FeeReceivedContract.Model model, FeeReceivedContract.View rootView) {
        super(model, rootView);
    }

    public void init() {
        if("今日已收".equals(mRootView.getActivity().getIntent().getStringExtra(AppConst.DUE_RECEIVED))){
            showLabel = false;
        }
        getDate();
    }

    private void getDate(){
//        for (int i = 1; i <= 10; i++) {
//            PropertyCharge.Total total = new PropertyCharge.Total(
//                    "丁当社区1幢1单元101室-" + i,
//                    i + 100,
//                    "张" + i,
//                    "1866871993" + i,
//                    "今天");
//            mList.add(total);
//        }
//
//        for (int i = 1; i <= 10; i++) {
//            PropertyCharge.Total total = new PropertyCharge.Total(
//                    "丁当社区1幢1单元101室-" + i,
//                    i + 100,
//                    "张" + i,
//                    "1866871993" + i,
//                    "11月22日");
//            mList.add(total);
//        }
//
//        for (int i = 1; i <= 10; i++) {
//            PropertyCharge.Total total = new PropertyCharge.Total(
//                    "丁当社区1幢1单元101室-" + i,
//                    i + 100,
//                    "张" + i,
//                    "1866871993" + i,
//                    "11月21日");
//            mList.add(total);
//        }
//
//        for (int i = 1; i <= 9; i++) {
//            PropertyCharge.Total total = new PropertyCharge.Total(
//                    "丁当社区1幢1单元101室-" + i,
//                    i + 100,
//                    "张" + i,
//                    "1866871993" + i,
//                    "11月20日");
//            mList.add(total);
//        }
//
//        mAdapter = new FeeRecordViewAdapter(mList);
//        mRootView.getListview().setAdapter(mAdapter);
//
//        setLabel();

    }

    /**
     * 这个是用来取出同一时间用的
     * @param list
     */
    private void getDataItem(List<PropertyCharge.Total> list){
        for (PropertyCharge.Total total : list) {
            if(mTotalDataMaps.containsKey(total.paymentDate)){
                PropertyCharge.TotalData totalData = mTotalDataMaps.get(total.paymentDate);
                totalData.addTotal(total);
            }else{
                mTotalDataMaps.put(total.paymentDate, new PropertyCharge.TotalData(total.total, total));
            }
        }
    }

    private void setLabel(){
        if(!showLabel){
            return;
        }

//        getDataItem(mList);

        LayoutInflater layoutInflater = LayoutInflater.from(mRootView.getActivity());
        View view = layoutInflater.inflate(R.layout.propertyfee_item_group,null);
        GroupItemDecoration groupItemDecoration = new GroupItemDecoration(mRootView.getActivity(), view, new GroupItemDecoration.DecorationCallback() {
            @Override
            public void setGroup(List<GroupItem> groupList) {
                //设置分组，例如：
                GroupItem groupItem;
                int startPosition = 0;
                for (PropertyCharge.Record4Client.FeeList total : mList) {
                    for (String key : mTotalDataMaps.keySet()) {
                        PropertyCharge.TotalData totalData = mTotalDataMaps.get(key);
                        if(total.equals(totalData.total)){
                            groupItem = new GroupItem(startPosition);
                            groupItem.setData("data",key);
                            groupItem.setData("total",totalData.price);
                            groupList.add(groupItem);
                            startPosition+=totalData.childPosition;
                        }
                    }
                }
            }

            @Override
            public void buildGroupView(View groupView, GroupItem groupItem) {
                //构建GroupView，通过view.findViewById找到内部控件，例如
                TextView tv_data = groupView.findViewById(R.id.tv_data);
                tv_data.setText(groupItem.getData("data").toString());
            }
        });
        mRootView.getListview().addItemDecoration(groupItemDecoration);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;

        this.mList = null;
        this.mAdapter = null;
        this.mTotalDataMaps = null;
    }
}
